package org.webflux.provider.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.webflux.provider.pojo.User;
import org.webflux.provider.repository.UserRepository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("users")
public class UserController {

    private UserRepository userRepository = new UserRepository();
    @RequestMapping("all")
    public Flux<User> findAll() {
        return userRepository.findAll();
    }
    @RequestMapping("{id}")
    public Mono<User> findById(@PathVariable("id") String id) {
        return userRepository.findById(id);
    }

//    @RequestMapping("add")
//    public Mono<String> createUser(ServerRequest serverRequest) {
//        // 获取用户提交数据
//        Mono<User> userMono = serverRequest.bodyToMono(User.class);
//        return userRepository.addUser(userMono);
//    }

    @PostMapping("add")
    public Mono<String> createUser(@RequestBody User user) {
        Mono<User> userMono = Mono.just(user);
        return userRepository.addUser(userMono);
    }
}
